package com.javakc.pms.instruction.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.javakc.commonutils.api.R;
import com.javakc.pms.instruction.entity.Instruction;
import com.javakc.pms.instruction.listener.ExcelListener;
import com.javakc.pms.instruction.service.InstructionService;
import com.javakc.pms.instruction.vo.InstructionData;
import com.javakc.servicebase.config.InstructionQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

@Api(tags="调度指令库管理")
@RestController
@RequestMapping("/pms/instruction")
@RefreshScope
public class InstructionController {

    @Autowired
    private InstructionService instructionService;

//    @Value("${javakc.test}")
//    private String test;

    @GetMapping
    public R findAll(){
        List<Instruction> list = instructionService.list();
        return R.OK().data("items",list);
    }

    @ApiOperation(value = "根据条件进行分页查询 - 调度指令库")
    @PostMapping("{pageNo}/{pageSize}")
    public R findPageIntruction(
            @ApiParam(name = "pageNo", value = "页码", required = true)
            @PathVariable("pageNo") Long pageNo,
            @ApiParam(name = "pageSize", value = "每页最大条数", required = true)
            @PathVariable("pageSize") Long pageSize,
            @ApiParam(name = "instructionQuery", value = "查询条件封装对象", required = false)
            @RequestBody(required = false) InstructionQuery instructionQuery) {
        Page<Instruction> instructionPage  = new Page<>(pageNo, pageSize);
        instructionService.findPageInstruction(instructionPage,instructionQuery);
        long total = instructionPage.getTotal();
        List<Instruction> instructionList = instructionPage.getRecords();
        return R.OK().data("total", total).data("items", instructionList);
    }

    @ApiOperation(value="新增调度指令库")
    @PostMapping("saveIntruction")
    public R saveIntruction(@RequestBody Instruction instruction){
        boolean flag   = instructionService.save(instruction);
        if(flag)
        {
            return R.OK();
        }else{
            return R.ERROR();
        }
    }

    @ApiOperation(value = "根据ID获取调度指令库")
    @GetMapping("getInstructionByid/{instructionId}")
    public R getIntructionById(@PathVariable("instructionId") int instructionId){
        Instruction instruction = instructionService.getById(instructionId);
        return R.OK().data("instruction",instruction);
    }

    @ApiOperation(value = "修改调度指令库")
    @PutMapping("updateInstruction")
    public R updateIntrucation(@RequestBody Instruction instruction){
        boolean flag = instructionService.updateById(instruction);
        if(flag)
            return R.OK();
        else
            return R.ERROR();
    }

    @ApiOperation(value="根据ID删除调度指令库")
    @DeleteMapping("deleteInstructionId/{instructionId}")
    public R deleteIntructionById(@PathVariable("instructionId")String instructionId){
        boolean flag = instructionService.removeById(instructionId);
        if(flag)
            return R.OK();
        else
            return R.ERROR();
    }

    /**
     * EasyExcel技术导出
     * @param response
     */
    @ApiOperation(value = "列表导出",notes = "使用阿里EasyExcel导出Excel格式的列表数据")
    @GetMapping("exprotEasyExcel")
    public void exportEasyExcel(HttpServletResponse response){
        //查询调度指令库
        List<Instruction> instructionList = instructionService.list();
        //定义导出列表集合
        List<InstructionData> instructionDataList = new ArrayList<>();

        for(Instruction instruction : instructionList){
            InstructionData instructionData = new InstructionData();
            BeanUtils.copyProperties(instruction,instructionData);
            instructionDataList.add(instructionData);
        }

        String fileName="instructionList";

        try {
            //设置相应信息
            response.reset();
            response.setContentType("application/vnd.ms-excel; charset=utf-8");
            response.setCharacterEncoding("utf-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "utf-8") + ".xlsx");
            EasyExcel.write(response.getOutputStream(),InstructionData.class).sheet("列表").doWrite(instructionDataList);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * EasyExcel技术导入
     */
    @ApiOperation(value = "列表导入",notes = "使用阿里巴巴EasyExcel技术进行的导入功能实现")
    @PostMapping("improtEasyExcel")
    public void importEasyExcel(MultipartFile file){
        // 创建一个监听器
        try {
            EasyExcel.read(file.getInputStream(),InstructionData.class,new ExcelListener(instructionService)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 使用POI技术进行导出
     * @param response
     */
    @ApiOperation(value = "列表导出",notes = "使用Apache POI导出Excel格式的列表数据")
    @GetMapping("exprotExcel")
    public void exportExcel(HttpServletResponse response){
        instructionService.exportExcel(response);
    }

    /**
     * POI技术导入
     */
    @ApiOperation(value = "列表导入",notes = "使用Apache POI技术进行的导入功能实现")
    @PostMapping("improtExcel")
    public void importExcel(MultipartFile file){
        instructionService.importExcel(file);
    }

}
